import time

from PIL import Image
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

# 分辨率
# https://www.cnblogs.com/jiyu-hlzy/p/12155738.html
# https://www.cnblogs.com/jiyu-hlzy/p/12155738.html

s = Service(r"./chromedriver.exe")
bro = webdriver.Chrome(service=s)
# bro.maximize_window()

bro.get('https://kyfw.12306.cn/otn/login/init')
time.sleep(1)

# save_screenshot就是将当前页面进行截图且保存
bro.save_screenshot('12306.png')

login_ele = bro.find_element(By.XPATH, '//*[@id="loginSub"]')
location = login_ele.location
print("location:", location)
size = login_ele.size
print("size:", size)

# 左上角和右下角坐标
xy = (int(location['x']), int(location['y']), int(location['x'] + size['width']), int(location['y'] + size['height']))
# 至此验证码图片区域就确定下来了
print(xy)

i = Image.open('./12306.png')
code_img_name = './login.png'
# crop根据指定区域进行图片裁剪
frame = i.crop(xy)
frame.save(code_img_name)

bro.quit()

# ActionChains(bro).move_to_element_with_offset(code_img_ele, x, y).click().perform()
# time.sleep(1)
